# -*- coding: utf-8 -*-
from Utils import *
from DB import DB
import urllib

class FactoryInfo:

    def __init__(self):
        self.id = ''
        self.name = ''
        self.link = ''
        self.rank = ''
        
class ProductInfo:

    def __init__(self):
        self.id = ''
        self.name = ''
        self.link = ''
        self.price = ''
        self.sellCount = ''
        self.leftCount = ''
        self.factory = FactoryInfo()
        
    def tidy(self, s):
        t = ''

        for c in s:
            if c in '\r\n\'"<>,\\':
                continue
            t += c

        return t.strip(' \t')

    def tidyAll(self):
        self.id = self.tidy(self.id)
        self.name = self.tidy(self.name)
        self.link = self.tidy(self.link)
        self.factory.id = self.tidy(self.factory.id)
        self.factory.name = self.tidy(self.factory.name)
        self.factory.link = self.tidy(self.factory.link)
        self.factory.rank = self.tidy(self.factory.rank)
        
        if self.price != '':
            self.price = '%.2f' % self.price

    def isValid(self):
        if self.id != '' and self.name != '' and self.link != '':
            return True

        return False

    def save(self, userid, task_id, keyword, platform):
        self.tidyAll()

        db = DB()
        db.Execute('INSERT INTO TaskProducts(UserId, TaskId, ProductType, Platform, ProductId, Description, Link, Price, LeftCount, SellCount, ShopId, ShopName, ShopLink, ShopLevel, AddTime) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, Now())', (userid, task_id, keyword, platform, self.id, self.name, self.link, self.price, self.leftCount, self.sellCount, self.factory.id, self.factory.name, self.factory.link, self.factory.rank))
        db.Commit()
        db.Close()

class Site:

    def __init__(self, cc):
        self.cc = cc
        self.website = ''
        self.websiteLink = ''
        self.pageLink = ''

    def searchHomePage(self, keyword, type = 'html'):
        if self.websiteLink == '' or keyword == '':
            return None

        return fetch_page(self.websiteLink % urllib.quote(keyword), type)

    def searchPage(self, keyword, page, type = 'html'):
        if self.pageLink == '' or keyword == '':
            return None

        return fetch_page(self.pageLink % (urllib.quote(keyword), page), type)

    def openPage(self, url, type = 'html', referer = ''):
        return fetch_page(url, type, referer)

    def getAll(self, keyword):
        return []

    def logDB(self, title, link, content):
        if not self.cc.Debug:
            return

        db = DB()
        db.Execute('INSERT INTO Logs(UserId, TaskId, Keyword, Platform, Title, Link, Content, LogTime) VALUES(%s, %s, %s, %s, %s, %s, %s, Now())', (self.cc.UserId, self.cc.TaskId, self.cc.Keyword, self.website, title, link, content))
        db.Commit()
        db.Close()

    def log(self, name, *args):
        if not self.cc.Debug:
            return

        if self.website == '' or name == '':
            return

        s = u''
        for x in args:
            s += unicode(x)

        file_name = './temp/%s_%s.log' % (self.website, name)
        with open(file_name, 'a+') as f:
            f.write((s + '\n').encode('utf-8'))

    def logProduct(self, product, err = False):
        s = ''
        s += 'id = %s\n' % product.id
        s += 'name = %s\n' % product.name
        s += 'link = %s\n' % product.link
        s += 'price = %s\n' % product.price
        s += 'sellCount = %s\n' % product.sellCount
        s += 'leftCount = %s\n' % product.leftCount
        s += 'factory.id = %s\n' % product.factory.id
        s += 'factory.name = %s\n' % product.factory.name
        s += 'factory.link = %s\n' % product.factory.link
        s += 'factory.rank = %s\n' % product.factory.rank

        if err:
            self.log('product_error', s)
        else:
            self.log('product', s)

    def logException(self, e):
        self.log('exception', str(e))
